package com.dhcc.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.dhcc.dto.TrainTeaRel;
import com.dhcc.model.TrainPlan;
import com.dhcc.service.TrainPlanService;
import com.dhcc.utils.Pager;

/**
 * Created by guorui on 2017/3/8.
 */

@Controller
@RequestMapping("/train")
public class TrainPlanController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private TrainPlanService tpService;

	@RequestMapping("/trainManager")
	public String trainManager() {
		return "teacher/train_manager";
	}

	/**
	 * @return
	 */
	@RequestMapping("/add")
	public String addTrainPlan(TrainPlan tp) {

		tpService.save(tp);
		return "teacher/train_manager";
	}
	
	@RequestMapping("/datchdelete")
	public String datchdelete(){
		return null;
		
	}
	
	//删除单条记录
	@RequestMapping(value = "/{id}/delete",method = RequestMethod.GET)
	public String deleteOne(@PathVariable("id")long id ,
			@RequestParam(value="currentPage",defaultValue="1") int currentPage,
			HttpServletRequest request){
		logger.debug("删除的id是:{}" ,id);
		TrainPlan tp = tpService.get(id);
		tpService.delete(tp);
		
		return "redirect:/teacher/list?currentPage=" + currentPage;
	}
	
	//分页查询
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public ModelAndView listTrain(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage) {
		Pager<TrainTeaRel> pager = tpService.queryPage(currentPage);

		if (pager.getList().size() == 0 && currentPage != 1) {
			return  new ModelAndView("teacher/train_manager?currentPage=1");
		}

		return new ModelAndView("teacher/train_manager", "pager", pager);
	}

}
